package com.woniuxy.education.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.education.entity.*;
import com.woniuxy.education.mapper.ArticleMapper;
import com.woniuxy.education.mapper.StudentMapper;
import com.woniuxy.education.service.StudentService;
import com.woniuxy.education.utils.IsCollect;
import com.woniuxy.education.utils.SelectTitleUtils;
import com.woniuxy.education.utils.StringReorder;
import com.woniuxy.education.utils.Titles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ArticleMapper articleMapper;

    /**
     * 查询作业列表然后分页
     */
    @Override
    public PageResult<List<Homework>> findHomework(SelectWork selectWork) {
        PageResult<List<Homework>> result = new PageResult<>();
        Page<Object> startPage = PageHelper.startPage(selectWork.getPage(),selectWork.getSize());
        List<Homework> homeworks = studentMapper.findHomework(selectWork);
        System.out.println(homeworks);
        result.setCurrentPage(selectWork.getPage());
        result.setSize(selectWork.getSize());
        result.setTotal(startPage.getTotal());
        result.setTotalPage(startPage.getPages());
        result.setData(homeworks);
        return result;
    }

    /**
     * 查询作业
     *
     * @param id
     * @return
     */
    @Override
    public AllTitle findAllTitle(int id) {
        Titleids titleids =studentMapper.findhomework_titleid(id);
        return Titles.allTitle(titleids);
    }

    /**
     * 将提交的作业插入进去
     * @param alLanswer
     * @return
     */
    @Override
    public boolean insertCompleted_work(ALLanswer alLanswer) {
        /**
         * 将题目插入到题目实现类中,在插到数据库
         */
        if(alLanswer.getState()==1){
            //题目的状态不应该影响作业的提交
            Completed_work completed_work=new Completed_work();
            completed_work.setH_id(alLanswer.getHomeortestid());
            completed_work.setChoose_answer(alLanswer.getListCTanswer());
            completed_work.setCompletion_answer(alLanswer.getListCLTanswer());
            completed_work.setComposition_answer(alLanswer.getListCSTanswer());
            completed_work.setAnswer_questions_answer(alLanswer.getListAQTanswer());
            completed_work.setU_id(alLanswer.getUserid());
            completed_work.setState(1);
            if(!studentMapper.insertCompleted_work(completed_work)){
                return false;
            }
        }else if(alLanswer.getState()==2){
            //题目的状态不应该影响作业的提交
            Completed_test completed_test=new Completed_test();
            completed_test.setT_id(alLanswer.getHomeortestid());
            completed_test.setChoose_answer(alLanswer.getListCTanswer());
            completed_test.setCompletion_answer(alLanswer.getListCLTanswer());
            completed_test.setComposition_answer(alLanswer.getListCSTanswer());
            completed_test.setAnswer_questions_answer(alLanswer.getListAQTanswer());
            completed_test.setU_id(alLanswer.getUserid());
            if(!studentMapper.insertCompleted_test(completed_test)){
                return false;
            }
        }

        /**
         * 将已写的选择题插入题库,
         * 先将已做的题目字符串查出来,相加,去重
         * UpdateChooseIsdp第一个参数是传回来的题目,第二个是数据库查出来的
         */
        Allquestionsid allquestionsid=alLanswer.getAllquestionsid();
        Topics_table topics_table=studentMapper.SelectTopics_table(alLanswer.getUserid());
        //选择题
        if(allquestionsid.getCtid()!=null){
            String ChooseString = StringReorder.StringSort(allquestionsid.getCtid(),topics_table.getChoose_isdo());
            System.out.println(ChooseString);
            studentMapper.UpdateChooseIsdo(ChooseString,alLanswer.getUserid());
        }
        //填空题
        if(allquestionsid.getCltid()!=null){
            String CompletionString = StringReorder.StringSort(allquestionsid.getCltid(),topics_table.getCompletion_isdo());
            studentMapper.UpdateCompletionIsdo(CompletionString,alLanswer.getUserid());
        }
        //解答题
        if(allquestionsid.getAqtid()!=null){
            String Answer_QuestionString = StringReorder.StringSort(allquestionsid.getAqtid(),topics_table.getAnswer_questions_isdo());
            studentMapper.UpdateAnswerQuestionsIsdo(Answer_QuestionString,alLanswer.getUserid());
        }
        //作文题
        if(allquestionsid.getCstid()!=null){
            String CompositionString = StringReorder.StringSort(allquestionsid.getCstid(),topics_table.getComposition_isdo());
            studentMapper.UpdateCompositionIsdo(CompositionString,alLanswer.getUserid());
        }
        return true;
    }
    @Override
    public boolean updateHomeworkstatus(int homeortestid){
        return studentMapper.updateHomeworkstatus(homeortestid);
    }

    @Override
    public boolean updateTeststatus(int homeortestid) {
        return studentMapper.updateTeststatus(homeortestid);
    }


    /**
     * 查询考试条目
     * @param c_id
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<List<TestPage>> findtestPage(int c_id, int page, int size) {
        PageResult<List<TestPage>> result = new PageResult<>();
        Page<Object> startPage = PageHelper.startPage(page, size);
        List<TestPage> testPages = studentMapper.findtestPage(c_id);
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(startPage.getTotal());
        result.setTotalPage(startPage.getPages());
        result.setData(testPages);
        return result;
    }

    /**
     * 查询具体的题目
     * @param id
     * @return
     */
    @Override
    public AllTitle findtestTitle(int id) {
        Titleids titleids =studentMapper.findtest_titleid(id);
        return Titles.allTitle(titleids);
    }

    @Override
    public boolean insertquestion(Question_ask question_ask){
        return studentMapper.insertquestion(question_ask);
    }

    @Override
    public PageResult<List<Question_ask>> findAllQuestion(int page, int size) {
        PageResult<List<Question_ask>> result = new PageResult<>();
        Page<Object> startPage = PageHelper.startPage(page, size);
        List<Question_ask> question_asks = studentMapper.findAllQuestion();
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(startPage.getTotal());
        result.setTotalPage(startPage.getPages());
        result.setData(question_asks);
        return result;
    }
    @Override
    public List<Student> findStudentsByC_id(Integer c_id){
        return studentMapper.findStudentsByC_id(c_id);
    }

    @Override
    public ListTestScore findTestData(int level, int userid, int c_id) {
        //查询学生的所有考试成绩
        ListTestScore listTestScore=new ListTestScore();
        List<Test_score> test_scores=studentMapper.findTestScore(userid);
        //查询该班级的平均成绩
        listTestScore.setTest_score(test_scores);
        List<Class_adv> class_adv=studentMapper.findClass_adv(level,c_id);
        listTestScore.setClass_adv(class_adv);
        //查询年级的平均成绩
        List<Grade_adv> grade_adv=studentMapper.findGrade_adv(level);
        listTestScore.setGrade_adv(grade_adv);
        return listTestScore;
    }



    /**
     *
     * @param u_id
     * @return //将已做的题遍历出来
     */
    @Override
    public List<Allquestions> findquestionisdo(int u_id) {
        //查询出题目
        Topics_table topics_table=studentMapper.SelectTopics_table(u_id);
        List<Allquestions> allquestions=new ArrayList<>();
        //添加选择题
        //添加填空题
        SelectTitleUtils.Allques( topics_table.getCompletion_isdo(),2,allquestions,topics_table.getCompletion_iserror());
        //添加解答题
        SelectTitleUtils.Allques( topics_table.getAnswer_questions_isdo(),3,allquestions,topics_table.getAnswer_questions_iserror());
        //添加作文题
        SelectTitleUtils.Allques( topics_table.getComposition_isdo(),4,allquestions,topics_table.getComposition_iserror());
        return allquestions;
    }

    /**
     *
     * @param titleid 题目id
     * @param u_id    用户id
     * @param titleType 题目类型 1:选择题,2:填空题,3:解答题,4:作文题
     * @return
     */
    @Override
    public boolean collectiontitle(int titleid, int u_id, int titleType) {
        //判断题目类型
        if(titleType==1){
            String choose=studentMapper.findCollectChoose(u_id);
            //如果为真,则表示该题目收藏过
            if(IsCollect.IsCollect(titleid,choose)){
                return true;
            }
            else {
                //调用方法排序然后添加
                return studentMapper.UpdateCollectChoose(StringReorder.StringSort(titleid,choose),u_id);
            }
        }
        if(titleType==2){
            String completion=studentMapper.findCollectCompletion(u_id);
            //如果为真,则表示该题目收藏过
            if(IsCollect.IsCollect(titleid,completion)){
                return true;
            }
            else {
                //调用方法排序然后添加
                return studentMapper.UpdateCollectCompletion(StringReorder.StringSort(titleid,completion),u_id);
            }
        }
        if(titleType==3){
            String answer_questions=studentMapper.findCollectAnswer_Questions(u_id);
            //如果为真,则表示该题目收藏过
            if(IsCollect.IsCollect(titleid,answer_questions)){
                return true;
            }
            else {
                //调用方法排序然后添加
                return studentMapper.UpdateCollectAnswer_Questions(StringReorder.StringSort(titleid,answer_questions),u_id);
            }
        }
        if(titleType==4){
            String composition=studentMapper.findCollectComposition(u_id);
            //如果为真,则表示该题目收藏过
            if(IsCollect.IsCollect(titleid,composition)){
                return true;
            }
            else {
                //调用方法排序然后添加
                return studentMapper.UpdateCollectComposition(StringReorder.StringSort(titleid,composition),u_id);
            }
        }
        return false;
    }

    /**
     * 根据收藏的题目id查出题目
     * @param u_id
     * @return
     */
    @Override
    public List<Allquestions> findCollectTitle(int u_id) {
        Collectiontitles collectiontitles=studentMapper.findCollectTitle(u_id);
        List<Allquestions> allquestions=new ArrayList<>();
        //添加选择题
        SelectTitleUtils.Allques( collectiontitles.getChoose(),1,allquestions);
        //添加填空题
        SelectTitleUtils.Allques( collectiontitles.getCompletion(),2,allquestions);
        //添加解答题
        SelectTitleUtils.Allques( collectiontitles.getAnswer_questions(),3,allquestions);
        //添加作文题
        SelectTitleUtils.Allques( collectiontitles.getComposition(),4,allquestions);
        return allquestions;
    }

    /**
     * 取消收藏
     * @param titleid
     * @param u_id
     * @param titleType
     * @return
     */
    @Override
    public boolean cancelcollect(int titleid, int u_id, int titleType) {
        if(titleType==1) {
            String choose=studentMapper.findCollectChoose(u_id);
            return studentMapper.UpdateCollectChoose(StringReorder.cancelcollect(titleid,choose), u_id);
        }
        if(titleType==2) {
            String completion=studentMapper.findCollectCompletion(u_id);
            return studentMapper.UpdateCollectCompletion(StringReorder.cancelcollect(titleid,completion), u_id);
        }
        if(titleType==3) {
            String answer_questions=studentMapper.findCollectAnswer_Questions(u_id);
            return studentMapper.UpdateCollectAnswer_Questions(StringReorder.cancelcollect(titleid,answer_questions), u_id);
        }
        if(titleType==4) {
            String composition=studentMapper.findCollectComposition(u_id);
            return studentMapper.UpdateCollectComposition(StringReorder.cancelcollect(titleid,composition), u_id);
        }
        return false;
    }

    @Override
    public PageResult<List<Article>> findPassArticle(int page,int size) {
        PageResult<List<Article>> result = new PageResult<>();
        Page<Object> startPage = PageHelper.startPage(page, size);
        List<Article> articles = articleMapper.findArticleByState(1);
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(startPage.getTotal());
        result.setTotalPage(startPage.getPages());
        result.setData(articles);
        return result;
    }

    @Override
    public AllTitle brushTopic(AllClassify allClassify) {
        //如果是1,表示学生想刷的是选择题
        if(Integer.parseInt(allClassify.getTitleType())==1){
            AllTitle allTitle=new AllTitle();
            List<Choose_table> choose_tables=new ArrayList<>();
            //如果是1,代表刷全部的题
            if(Integer.parseInt(allClassify.getRange())==1){
                choose_tables=studentMapper.findGradeToChoose(allClassify.getSubject(),allClassify.getGrade());
                allTitle.setListCT(choose_tables);
                return allTitle;
            }
            //如果是2,代表刷错题
            if(Integer.parseInt(allClassify.getRange())==2){
                String errortitle=studentMapper.findIsErrorChoose(allClassify.getU_id());
                // 正则“,”切割字符串
                String[] splitArrays = errortitle.split(",");
                for(int i=0;i<splitArrays.length;i++){
                    Choose_table choose_table=studentMapper.findChoose(Integer.parseInt(splitArrays[i]));
                    choose_tables.add(choose_table);
                }
                allTitle.setListCT(choose_tables);
                return allTitle;
            }
            //如果是3,代表刷没有做过的题目
            if(Integer.parseInt(allClassify.getRange())==3){
                //查询已做的选择题
                String choose_isdo=studentMapper.findIsdoChoose(allClassify.getU_id());
                //根据年级和科目查询对应的所有选择题id
                List<Integer> listchooseids=studentMapper.findChooseIds(allClassify.getSubject(),allClassify.getGrade());
                //去重
                listchooseids=StringReorder.outIsDo(choose_isdo,listchooseids);

                for(int i=0;i<listchooseids.size();i++){
                    Choose_table choose_table=studentMapper.findChoose(listchooseids.get(i));
                    choose_tables.add(choose_table);
                }
                allTitle.setListCT(choose_tables);
               return allTitle;
            }
        }
        return  null;
    }

    @Override
    public boolean endOfDoTitle(AllClassify allClassify) {
        Topics_table topics_table=studentMapper.SelectTopics_table(allClassify.getU_id());
        //如果是一,插入选择题
        if(Integer.parseInt(allClassify.getTitleType())==1){
            //插入已做的
            String ChooseString = StringReorder.StringSort(allClassify.getTitleids(),topics_table.getChoose_isdo());
            System.out.println(ChooseString);
            boolean isdo = studentMapper.UpdateChooseIsdo(ChooseString,allClassify.getU_id());
            //插入做错了的题
            String ChooseErrorString = StringReorder.StringSort(allClassify.getErrortitleids(),topics_table.getChoose_iserror());
            System.out.println(ChooseErrorString);
            boolean iserror= studentMapper.UpdateChooseIsError(ChooseErrorString,allClassify.getU_id());
            if(isdo&&iserror){
                return true;
            }
        }
        return false;
    }
}
